package edu.zzuli.common;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author tianshaojie
 * @date 2011-8-19
 * @discription : 对/view路径下的所有请求都会接受SecurityFilter的检查
 */
public class SecurityFilter implements Filter {

	public void destroy() {

	}

	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		HttpServletRequest req = (HttpServletRequest) request;
		HttpServletResponse res = (HttpServletResponse) response;

		Visit myVisit = (Visit) req.getSession().getAttribute("myVisit");
		if (myVisit == null || !myVisit.isLogin()) {
			res.sendRedirect(req.getContextPath() + "/index.jsp");
	    }
	}

	public void init(FilterConfig arg0) throws ServletException {

	}
}
